#include "StdAfx.h"
#include ".\winmedia.h"

CWinMedia::CWinMedia(void)
: _plPos(0)
, _plCurrURL(_T(""))
{
}

CWinMedia::~CWinMedia(void)
{
}

int CWinMedia::Play(void) {
	HRESULT res;
	CComPtr<IWMPControls>   spControls;

	if(m_spPlayer.p != NULL)
		m_spPlayer->get_controls(&spControls);
	if(spControls.p != NULL) {
		res = spControls->play();
	}
	return (int) res;
}

int CWinMedia::Stop(void) {
	HRESULT res = NULL;
	CComPtr<IWMPControls>   spControls;
	CComPtr<IWMPCore> spCore;

	if(m_spPlayer.p != NULL)
		m_spPlayer->get_controls(&spControls);
	if(spControls.p != NULL) {
		res = spControls->stop();
	}
	if (m_spPlayer.p != NULL) {
		m_spPlayer->close();
	}
	return (int) res;
}

int CWinMedia::Previous(void) {
	HRESULT res;
	CComPtr<IWMPControls>   spControls;

	if(m_spPlayer.p != NULL)
		m_spPlayer->get_controls(&spControls);
	if(spControls.p != NULL) {
		res = spControls->previous();
	}
	return (int) res;
}

int CWinMedia::Next(void) {
	HRESULT res;
	CComPtr<IWMPControls>   spControls;

	if(m_spPlayer.p != NULL)
		m_spPlayer->get_controls(&spControls);
	if(spControls.p != NULL) {
		res = spControls->next();
	}
	return (int) res;
}

int CWinMedia::SetPosition(int ms) {
	HRESULT res;
	CComPtr<IWMPControls>   spControls;

	if(m_spPlayer.p != NULL)
		m_spPlayer->get_controls(&spControls);
	if(spControls.p != NULL) {
		res = spControls->put_currentPosition(ms);
	}
	return (int) res;
}

int CWinMedia::GetPosition(void) {
	HRESULT hRes;
	double res = -1;
	CComPtr<IWMPControls>   spControls;

	if(m_spPlayer.p != NULL)
		m_spPlayer->get_controls(&spControls);
	if(spControls.p != NULL) {
		hRes = spControls->get_currentPosition(&res);
	}
	return (int) res;
}


int CWinMedia::Refresh(char *file, int pos) {
	HRESULT                 hr = E_POINTER;
	CComPtr<IWMPPlaylist>   spPlaylist;
	CComPtr<IWMPMedia>   spMedia;
	CComPtr<IWMPMediaCollection> spMediaColl;

	if(m_spPlayer.p != NULL)
		hr = m_spPlayer->get_currentPlaylist(&spPlaylist);
	if (SUCCEEDED(hr) && spPlaylist.p != NULL) {
		hr = spPlaylist->get_item(pos, &spMedia);
	}
	if (SUCCEEDED(hr) && spPlaylist.p != NULL) {
		hr = m_spPlayer->get_mediaCollection(&spMediaColl);
	}
	// Remove the item from the playlist
	if (SUCCEEDED(hr) && spPlaylist.p != NULL)
		hr = spPlaylist->removeItem(spMedia);
	// Remove the item from the media collection
	if (SUCCEEDED(hr) && spMediaColl.p != NULL)
		hr = spMediaColl->remove(spMedia, VARIANT_FALSE);
	// Add the item to the collection and set the position	
	PlAdd(file, pos);
	PlSetPos(pos);
	return 0;
}


int CWinMedia::PlAdd(char *file, int pos) {
	HRESULT                 hr = E_POINTER;
	CComPtr<IWMPPlaylist>   spPlaylist;
	CComPtr<IWMPMedia>   spMedia;

	
	if(m_spPlayer.p != NULL)
		hr = m_spPlayer->get_currentPlaylist(&spPlaylist);
	if (SUCCEEDED(hr)) {
		CComBSTR bstrFile(file);
		hr = m_spPlayer->newMedia(bstrFile, &spMedia);
	}
	if (SUCCEEDED(hr) && spPlaylist.p != NULL) {
		hr = spPlaylist->insertItem(pos, spMedia);
	}
	return (int) hr;
}

int CWinMedia::PlRemove(int pos) {
	HRESULT                 hr = E_POINTER;
	CComPtr<IWMPPlaylist>   spPlaylist;
	CComPtr<IWMPMedia>   spMedia;

	if(m_spPlayer.p != NULL)
		hr = m_spPlayer->get_currentPlaylist(&spPlaylist);
	if (SUCCEEDED(hr) && spPlaylist.p != NULL) {
		hr = spPlaylist->get_item(pos, &spMedia);
	}
	if (SUCCEEDED(hr) && spPlaylist.p != NULL)
		hr = spPlaylist->removeItem(spMedia);
	return (int) hr;
}
int CWinMedia::PlSetPos(int pos) {
	HRESULT                 hr = E_POINTER;
	CComPtr<IWMPPlaylist>   spPlaylist;
	CComPtr<IWMPMedia>   spMedia;
	CComPtr<IWMPControls>   spControls;

	if(m_spPlayer.p != NULL)
		hr = m_spPlayer->get_controls(&spControls);
	if(SUCCEEDED(hr) && m_spPlayer.p != NULL)
		hr = m_spPlayer->get_currentPlaylist(&spPlaylist);
	if (SUCCEEDED(hr) && spPlaylist.p != NULL) {
		hr = spPlaylist->get_item(pos, &spMedia);
	}
	if(spControls.p != NULL) {
		hr = spControls->put_currentItem(spMedia);
	}
	return (int) hr;
}


int CWinMedia::PlGetPos(void) {
	HRESULT                 hr = E_POINTER;
	CComPtr<IWMPPlaylist>   spPlaylist;
	CComPtr<IWMPMedia>   spMedia;
	CComPtr<IWMPMedia>   spMediaPl;
	CComPtr<IWMPControls>   spControls;
	VARIANT_BOOL equal;
	CComBSTR name;

	if(m_spPlayer.p != NULL)
		hr = m_spPlayer->get_controls(&spControls);
	if(SUCCEEDED(hr) && m_spPlayer.p != NULL)
		hr = m_spPlayer->get_currentPlaylist(&spPlaylist);
	if(spControls.p != NULL && spPlaylist != NULL) {
		hr = spControls->get_currentItem(&spMedia);
	}
	if (spMedia.p != NULL) {
		hr = spMedia->get_sourceURL(&name);
		if (SUCCEEDED(hr) && name.m_str == _plCurrURL)
			return _plPos;
		_plCurrURL = name;
	}

	_plPos = -1;
	if (SUCCEEDED(hr) && spPlaylist.p != NULL && spMedia.p != NULL) {
		for (int i = 0, size = PlGetSize(); i < size; i++) {
			spPlaylist->get_item(i, &spMediaPl);
			spMedia->get_isIdentical(spMediaPl, &equal);
			spMediaPl.Release();
			if (equal == VARIANT_TRUE) {
				_plPos = i;
				return _plPos;
			}
		}
	}
	return _plPos;
}

int CWinMedia::PlGetSize(void) {
	HRESULT                 hr = E_POINTER;
	CComPtr<IWMPPlaylist>   spPlaylist;
	long count = 0;

	if(m_spPlayer.p != NULL)
		hr = m_spPlayer->get_currentPlaylist(&spPlaylist);
	if (SUCCEEDED(hr) && spPlaylist.p != NULL)
		hr = spPlaylist->get_count(&count);
	return (int) count;
}

int CWinMedia::IsPlaying(void) {
	HRESULT res;
	WMPPlayState state;

	if (m_spPlayer.p != NULL)
		res = m_spPlayer->get_playState(&state);
	if (SUCCEEDED(res) && state > 2 && state < 7)
		return TRUE;
	else
		return FALSE;
}

CString CWinMedia::GetFileName(int pos) {
	HRESULT                 hr = E_POINTER;
	CComPtr<IWMPMedia>   spMedia;
	CComPtr<IWMPPlaylist>   spPlaylist;
	CComBSTR name;

	if(m_spPlayer.p != NULL)
		hr = m_spPlayer->get_currentPlaylist(&spPlaylist);
	if (SUCCEEDED(hr) && spPlaylist.p != NULL) {
		hr = spPlaylist->get_item(pos, &spMedia);
	}
	if (SUCCEEDED(hr) && spMedia.p != NULL) {
		hr = spMedia->get_sourceURL(&name);
	}
	return (CString) name;
}

int CWinMedia::Find(void) {
	HRESULT res = E_POINTER;
	VARIANT_BOOL state = FALSE;
	CComPtr<IWMPPlayerApplication>  spPlayerApp;

	if (m_spPlayer.p != NULL)
		res = m_spPlayer->get_playerApplication(&spPlayerApp);
	if (SUCCEEDED(res))
		res = spPlayerApp->get_playerDocked(&state);
	if (SUCCEEDED(res) && state == VARIANT_FALSE)
		return TRUE;
	else
		return FALSE;
}
